package com.beiing.tianshuai.tianshuai.dongtai.presenter;

import com.beiing.tianshuai.tianshuai.base.BasePresenter;
import com.beiing.tianshuai.tianshuai.dongtai.CallBack;
import com.beiing.tianshuai.tianshuai.dongtai.model.DynamicModel;
import com.beiing.tianshuai.tianshuai.dongtai.model.DynamicModelImpl;
import com.beiing.tianshuai.tianshuai.dongtai.view.DynamicViewImpl;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicAddRelayCommentBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicAddReplyBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicCommentAndReplyBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicCommentBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicDeleteBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicPraiseBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicShieldBean;
import com.beiing.tianshuai.tianshuai.entity.FollowBean;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

/**
 * Created by Jinni on 2017/7/17 0017.
 */

public class DynamicPresenter implements DynamicPresenterImpl {

    private DynamicModelImpl mDynamicModel;
    private DynamicViewImpl mView;

    public DynamicPresenter(DynamicViewImpl mView) {
        this.mView = mView;
        mDynamicModel = new DynamicModel();
    }

    @Override
    public void getDynamicsResult(String id, int start, int num, String dynamicsId, String relayId, final int type) {
        mView.showProgress();
        mDynamicModel.getDynamicsResult(id, start, num, dynamicsId, relayId, new CallBack<DynamicBean>() {
            @Override
            public void result(DynamicBean bean) {
                mView.hideProgress();
                if (type == 0){     //普通获取数据(首次加载、上拉加载)
                    mView.showDynamicList(bean);
                } else {            //下拉刷新and发布
                    mView.refreshDynamicList(bean);
                }
            }

            @Override
            public void error(Throwable e) {
                mView.hideProgress();
                mView.requestError(type);
            }
        });
    }

    @Override
    public void praiseDynamic(String userid, final String aid, final String to_uid, final int position) {
        mDynamicModel.praiseDynamic(userid, aid, to_uid, new CallBack<DynamicPraiseBean>() {
            @Override
            public void result(DynamicPraiseBean bean) {
//                mDynamicView.showPraise(bean, position);
                LogUtils.i(bean.getData().toString()+bean.getCode(), "praiseDynamic");
                sendPush("点赞", Integer.parseInt(to_uid), Integer.parseInt(aid), "dynamic");
            }

            @Override
            public void error(Throwable e) {
                mView.showError(1);
            }
        });
    }

    @Override
    public void praiseRelay(String uid, final String rid, String did, final String to_uid, int position) {
        mDynamicModel.praiseRelay(uid, rid, did, to_uid, new CallBack<DynamicPraiseBean>() {

            @Override
            public void result(DynamicPraiseBean bean) {
                LogUtils.i(bean.getData().toString()+bean.getCode(), "praiseRelay");
                if (bean.getCode() == 200){
                    sendPush("点赞", Integer.parseInt(to_uid), Integer.parseInt(rid), "dynamic");
                }
            }

            @Override
            public void error(Throwable e) {
                mView.showError(2);
            }
        });
    }

    @Override
    public void shieldDynamic(String uid, String did, final int position) {
        mDynamicModel.shieldDynamic(uid, did, new CallBack<DynamicShieldBean>() {

            @Override
            public void result(DynamicShieldBean bean) {
                mView.updateDynamic(position);
            }

            @Override
            public void error(Throwable e) {
                mView.showError(3);
            }
        });
    }

    @Override
    public void deleteDynamic(String uid, String did, final int position) {
        mDynamicModel.deleteDynamic(uid, did, new CallBack<CommonBean>() {

            @Override
            public void result(CommonBean bean) {
                mView.updateDynamic(position);
            }

            @Override
            public void error(Throwable e) {
                mView.showError(4);
            }
        });
    }

    @Override
    public void deleteRelay(String uid, String rid, final int position) {
        mDynamicModel.deleteRelay(uid, rid, new CallBack<CommonBean>() {
            @Override
            public void result(CommonBean bean) {
                mView.updateDynamic(position);
            }

            @Override
            public void error(Throwable e) {
                mView.showError(5);
            }
        });
    }

    @Override
    public void addComment(String uid, final String to_uid, final String aid, final String content, String register_id, final String create_time, final int position, final DynamicBean.DataBean.DynamicsBean dynamicBean) {
        mDynamicModel.addComment(uid, to_uid, aid, content, register_id, create_time, new CallBack<DynamicCommentBean>() {

            @Override
            public void result(DynamicCommentBean bean) {
                LogUtils.i(bean.getData().getContent(), "评论");
                mView.showCommentResult(bean, create_time, position, dynamicBean);
                sendPush("评论", Integer.parseInt(to_uid), Integer.parseInt(aid), "dynamic");
            }

            @Override
            public void error(Throwable e) {
                LogUtils.i(e.toString(), "评论");
            }
        });
    }

    @Override
    public void addRelayComment(final String rid, String uid, String did, final String to_uid, final String content, final int position, final DynamicBean.DataBean.DynamicsBean dynamicBean) {
        mDynamicModel.addRelayComment(rid, uid, did, to_uid, content, new CallBack<DynamicAddRelayCommentBean>() {

            @Override
            public void result(DynamicAddRelayCommentBean bean) {
                mView.showRelayCommentResult(bean, rid, content, position, dynamicBean);
                sendPush("评论", Integer.parseInt(to_uid), Integer.parseInt(rid), "dynamic");
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void addReply(final String did, String cid, String form_uid, final String to_uid, final String content, String registration_id, final int position, final int dynamicPosition, final DynamicBean.DataBean.DynamicsBean dynamicBean, final DynamicCommentAndReplyBean dynamicCommentAndReplyBean) {
        mDynamicModel.addReply(did, cid, form_uid, to_uid, content, registration_id, new CallBack<DynamicAddReplyBean>() {

            @Override
            public void result(DynamicAddReplyBean bean) {
                mView.showAddReplyResult(dynamicCommentAndReplyBean, dynamicPosition, dynamicBean, bean, content, did, position);
                sendPush("评论", Integer.parseInt(to_uid), Integer.parseInt(did), "评论");
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void addRelayReply(final String rid, String cid, String form_uid, final String to_uid,
                              final String content, String registration_id, final int position,
                              final int dynamicPosition,
                              final DynamicBean.DataBean.DynamicsBean dynamicBean,
                              final DynamicCommentAndReplyBean dynamicCommentAndReplyBean) {

        mDynamicModel.addRelayReply(rid, cid, form_uid, to_uid, content, registration_id, new CallBack<DynamicAddReplyBean>() {

            @Override
            public void result(DynamicAddReplyBean bean) {
                mView.showAddRelayReplyResult(dynamicCommentAndReplyBean, dynamicPosition, dynamicBean, bean, content, rid, position);
                sendPush("评论",Integer.parseInt(to_uid), Integer.parseInt(rid),"评论");
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void isFollowed(final String former_uid, final DynamicBean.DataBean.DynamicsBean bean, final int position) {
        final String latter_uid = bean.getFormUid() == null ? bean.getUid() : bean.getFormUid();
        mDynamicModel.isFollowed(former_uid, latter_uid, new CallBack<FollowBean>() {

            @Override
            public void result(FollowBean followBean) {
                mView.showCenterMenu(followBean, bean, position);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void setFollowed(String former_uid, String latter_uid) {
        mDynamicModel.setFollowed(former_uid, latter_uid, new CallBack<FollowResultBean>() {

            @Override
            public void result(FollowResultBean bean) {

            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void sendPush(String status, int tag, int id, String type) {
        mDynamicModel.sendPush(status, tag, id, type, new CallBack<DynamicDeleteBean>() {

            @Override
            public void result(DynamicDeleteBean bean) {

            }

            @Override
            public void error(Throwable e) {

            }
        });
    }
}
